package at.fhooe.mcm.view;

import java.awt.EventQueue;

import javax.swing.JLabel;
import javax.swing.JPanel;

import at.fhooe.mcm.logic.sensors.events.SensorChangedEvent;

@SuppressWarnings("serial")
public class SensorPanel extends JPanel {

	private JLabel m_nameField;
	private JLabel m_idField;
	private JLabel m_typeField;
	private JLabel m_valueField;
	/**
	 * Create the panel
	 */
	public SensorPanel(String _id, String _name, String _type, double _value)
	{
		super();
		setLayout(null);
		
		final JLabel nameLabel = new JLabel();
		nameLabel.setBounds(10, 10, 66, 16);
		nameLabel.setText("Name:");
		add(nameLabel);

		m_nameField = new JLabel();
		m_nameField.setText(_name);
		m_nameField.setBounds(82, 10, 138, 16);
		add(m_nameField);

		final JLabel idLabel = new JLabel();
		idLabel.setText("ID:");
		idLabel.setBounds(10, 32, 66, 16);
		add(idLabel);

		final JLabel typeLabel = new JLabel();
		typeLabel.setText("Type:");
		typeLabel.setBounds(10, 60, 66, 16);
		add(typeLabel);

		m_idField = new JLabel();
		m_idField.setText(_id);
		m_idField.setBounds(82, 32, 138, 16);
		add(m_idField);

		m_typeField = new JLabel();
		m_typeField.setText(_type);
		m_typeField.setBounds(82, 60, 138, 16);
		add(m_typeField);

		final JLabel valueLabel = new JLabel();
		valueLabel.setText("Value:");
		valueLabel.setBounds(10, 88, 66, 16);
		add(valueLabel);

		m_valueField = new JLabel();
		m_valueField.setText(""+_value);
		m_valueField.setBounds(82, 88, 138, 16);
		add(m_valueField);
	}
	
	public void setValue(final double _value)
	{
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					m_valueField.setText(""+_value);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public String getName()
	{
		return m_nameField.getText();
	}
	
	public String getID()
	{
		return m_idField.getText();
	}
	
	public String getType()
	{
		return m_typeField.getText();
	}
	
	public void changeData(final SensorChangedEvent _sce)
	{
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					m_nameField.setText(_sce.getName());
					m_typeField.setText(_sce.getEntityType());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

}
